<?php
/**
 * 项目：红河移动电路资源管理信息系统
 *
 * 公司：红河移动分公司
 *
 * @author 李朝应
 *
 * E-mail:lichaoying.hh@163.com
 *
 */
include("conf.inc.php");
class Main extends CI_Controller {
 /*
     * 构造函数
     */
 function __construct()
   {
        parent::__construct();
        $this->load->model('user_model','Muser');
        $this->load->model('event_model','evt');
        $this->load->model('type_model','tpe');
        $this->load->model('level_model','lev');
        $this->load->model('req_model',"req");
        $this->load->model('org1_model');
        $this->load->model('org2_model');
        $this->load->model('org3_model');
        $this->load->model('mail_model',"ml");
   }
   
    /*
     * 系统过程
     */

    function parm_1(){
        $conf = new conf();
        $data['title'] =$conf->title;
        $data['version'] =$conf->version."  For ".$conf->System_OS;
        $data['copyright']=$conf->copyright;
        $data['System_OS']=$conf->System_OS;
        $this->load->vars($data);
    }
   function SQLsafe( $string ) {   //阻止SQL注入式攻击,只适用PHP5
        return "'" . mysql_real_escape_string( $string ) . "'";
    }
   function SQLsafe2( $string ) {   //阻止SQL注入式攻击,只适用PHP5
        return  mysql_real_escape_string( $string );
    }
   function isLogin(){
    $user=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
    $pwd=$this->SQLsafe2($this->session->userdata('crms_userpwd_auth'));
    if($this->Muser->isSysytemUser($user,$pwd)){
        return TRUE;
    }else{
      return FALSE;
  }
  }
  function ifLogin(){
     if(!$this->isLogin()){
         redirect("main/login");
     }//判断登录状态
  }
  function hasLevel($str){
     $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
     if($this->lev->hasLev($login,$str)){
         return true;
     }else{
        redirect("main/error");
     }
  }
    /*
     * 系统页面
     */
    function index()
    {
        $this->ifLogin();//验证用户登录
        $this->parm_1();
        $this->load->view('index');

    }
    function error(){
        $this->ifLogin();//验证用户登录
        $this->parm_1();
        $this->load->view('error');
    }
    function top(){
        $this->ifLogin();//验证用户登录
        $this->parm_1();
        $user=$this->SQLsafe($this->session->userdata('crms_username_auth'));//阻止SQL注入式攻击
        $datas["userinfo"]=$this->Muser->getUserNameInfo($user);
        $datestring = " %H:%i:%s";
        $datestring1 = " %Y年%m月%d日";
        date_default_timezone_set('Asia/Shanghai');//北京时区
        $time = time();
        $this->load->helper('date');
        $datas["dates"]=mdate($datestring1, $time);
        $datas["nowtime"]=mdate($datestring, $time);
        $this->load->view('top',$datas);
    }
    function time_(){
    if($this->isLogin()){
       $this->load->helper('date');
       $datestring = " %H:%i:%s";
       date_default_timezone_set('Asia/Shanghai');//北京时区
       $time = time();
       echo mdate($datestring, $time);
     }else{
        echo("<span style='color: #C62E00;font-weight:bold;'><a href='".site_url("main/login")."'> 登录超时</a></span>");
       }
    }
    function menu(){
        $this->ifLogin();//验证用户登录
        $this->parm_1();
        $datas["type"]=$this->tpe->getTypeAll();
        $this->load->view('menu',$datas);
    }
    function Vmain(){
        $this->ifLogin();//验证用户登录
        $this->parm_1();
        $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
        $org1_id=$this->Muser->getUserOrg1ID($login);
        $datas["total"]=$this->req->getTotalDo($org1_id,$login);
        $this->load->view('Vmain',$datas);
    }
    function login(){
        $this->parm_1();
        $this->load->view("login");
    }
   function checklogin(){
       $error="";
       //取得表单数量
       $user=$this->security->xss_clean($this->input->post("username"));
       $pwd=$this->security->xss_clean($this->input->post("password"));
       if($this->Muser->isSysytemUser($user,$pwd)){
              $session_data = array('crms_username_auth'=>$user,'crms_userpwd_auth'=>$pwd); //写入到Session
	          $this->session->set_userdata($session_data);
               $this->evt->setUserEvent("登录成功");
              redirect("main/index");
          }else{
               redirect("main/login");
          }
        $this->parm_1();
        $msg['error'] = $error;
        $this->load->view("login",$msg);
   }
   function editpwd(){
        $this->ifLogin();//验证用户登录
        $this->parm_1();
        $this->load->view('editpwd');
   }
  function savepwd(){
        $this->ifLogin();//验证用户登录
        $error="";
        $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
        $old=$this->security->xss_clean($this->input->post('old'));
        $password=$this->security->xss_clean($this->input->post('password'));
       if($this->Muser->isSysytemUser($login,$old)){
          $this->Muser->setPwd($login,$password);
          $session_data = array('crms_username_auth'=>$login,'crms_userpwd_auth'=>$password); //写入到Session
	      $this->session->set_userdata($session_data);
          $this->evt->setUserEvent("更新密码成功");
          $error="<img src=\"images/ok.gif\" /><span class=\"about_content_title\"> 密码更新成功，己经生效!</span>";
       }else{
          $this->evt->setUserEvent("更新密码失败");
          $error="<img src=\"images/error.gif\" /><span class=\"about_content_title\"> 出错，原始密码不匹配!</span>";
     }
     echo $error;
  }
  function event($id=0){
    $this->ifLogin();//登录认证
    $this->hasLevel("+Q");//发送调单调度权根
    $this->parm_1();
    settype($id,"integer"); //设$id为整型
    $this->load->library('pagination');
    //自定义分页
    $config['first_link'] = '第一页';
    $config['last_link'] = '最后一页';
    $config['next_link'] = '下一页';
    $config['prev_link'] = '上一页';
    $config['cur_tag_open'] = '<b>&nbsp;';
    $config['cur_tag_close'] = '&nbsp;</b>';
    $config['full_tag_open'] = '页数：';
    $config['base_url'] = site_url("main/event");
    $config['total_rows'] = $this->evt->getUserEventTotal();
    $config['per_page'] = '30';
    //初始化分页参数
    $this->pagination->initialize($config);
    $datas['pages']=$this->pagination->create_links();
    $datas['event'] = $this->evt-> getUserEvent($id,30);
    $this->load->view('event',$datas);
   }
   function req($id,$type_id){
      $this->ifLogin();//验证用户登录
      $this->hasLevel("+R");//发送调单调度权根
      $this->parm_1();
      settype($type_id,"integer");
      settype($id,"integer");
      $datestring = " %Y%m";
      date_default_timezone_set('Asia/Shanghai');//北京时区
      $time = time();
      $this->load->helper('date');
      $datas["mon"] =mdate($datestring, $time);//按月归档
      $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
      $datas["user"]=$this->Muser->getUserByLogin($login);
      $datas["org1_id"]=$this->Muser->getUserOrg1ID($login);
      $datas["login"]=$login;
      $datas["type"]=$this->tpe->getTypeBy($type_id);
      if($id!="NULL"):
      $datas["req"]=$this->req->getReqByOrderID($id);
      endif;
      $this->load->view('req',$datas);
   }
   function req_con($id,$type_id){
      $this->ifLogin();//验证用户登录
      $this->hasLevel("+Q");//发送调单调度权根
      $this->parm_1();
      settype($type_id,"integer");
      settype($id,"integer");
      $datas["type"]=$this->tpe->getTypeBy($type_id);
      if($id!="NULL"):
      $datas["req"]=$this->req->getReqByOrderID2($id);
      endif;
      $this->load->view('req_con',$datas);
   }
   function orgselect(){
      $this->ifLogin();//验证用户登录
      $this->hasLevel("+R");//发送调单调度权根
      $this->parm_1();
      $user=$this->SQLsafe($this->session->userdata('crms_username_auth'));//阻止SQL注入式攻击
      $orgdata["userinfo"]=$this->Muser->getUserNameInfo($user);
      $orgdata['org1'] = $this->org1_model->getOrg1ALL();
      $this->load->view('orgselect',$orgdata);
   }
   function findOrg2($org1_id){
    $this->ifLogin();//登录认证
    echo $this->org2_model->getOrg2In2($org1_id);//返回一个Option列表

   }
   function findOrg3($org2_id){
    $this->ifLogin();//登录认证
    echo $this->org3_model->getOrg3In2($org2_id);//返回一个Option列表

   }
   function getReqData(){
       $this->ifLogin();//登录认证
       $this->hasLevel("+R");//发送调单调度权根
       $req_id=trim($this->input->post("req_id"));
       $org1_id=trim($this->input->post("org1_id"));
       $us_login=trim($this->input->post("user_login"));
       $state=trim($this->input->post("state"));
       $type=trim($this->input->post("type_id"));
       $cell_id=trim($this->input->post("cell_id"));
       $mon=trim($this->input->post("mon"));
       $main_send=trim($this->input->post("send"));
       $main_att=trim($this->input->post("mainsend"));
       $cause=trim($this->input->post("cause"));
       $main_content=trim($this->input->post("text"));
       $draft=trim($this->input->post("draft"));
       $tel=trim($this->input->post("tel"));
       $notify_time=trim($this->input->post("d1"));
       $complete_time=trim($this->input->post("d2"));
       $linkman=trim($this->input->post("linkman"));
       $to_1=trim($this->input->post("to1"));
       $to_2=trim($this->input->post("to2"));
       $router=trim($this->input->post("router"));
       $file_path="";
       //更新时间
        $datestring = " %Y-%m-%d %H:%i:%s";
        date_default_timezone_set('Asia/Shanghai');//北京时区
        $time = time();
        $this->load->helper('date');
        $update_time=mdate($datestring, $time);
        $reqdata = array(
               'Requisition_ID' => $req_id,
               'state' => $state,
               'type' => $type,
               'Cell_ID' => $cell_id,
               'org1_id'=>$org1_id,
               'us_login'=>$us_login,
               'Req_Mon'=>$mon,
               'Main_Send' =>$main_send,
               'Main_attemper' => $main_att,
                'Cause' => $cause,
               'Main_Content' => $main_content,
               'Draft' => $draft,
               'Tel' => $tel,
               'Notify_Time' => $notify_time,
               'Complete_Time' => $complete_time,
               'linkman' => $linkman,
               'To_1' => $to_1,
               'To_2' => $to_2,
               'Router' => $router,
               'File_Path' => $file_path,
               'update_time' =>$update_time,
            );
           return   $reqdata;
   }
  function setExcel_Win($req_id,$id_){
     $this->ifLogin();//登录认证
     //从数据库中提取数据
     $data=$this->req->getReqByID($req_id);
     //实例化配置文件
     $conf = new conf();
     //定义一个excel文件
      $workbook =$conf->crms_dir.$conf->req_model_file;
      $datestring = "%Y";
      $datestring1 = "%m";
      date_default_timezone_set('Asia/Shanghai');//北京时区
      $time = time();
      $this->load->helper('date');
      $year=trim(mdate($datestring, $time));
      $month=trim(mdate($datestring1, $time));
      $req_year=$conf->crms_dir.$conf->req_dir."/".$year."/";
      $req_month=$conf->crms_dir.$conf->req_dir."/".$year."/".$month."/";
      $req_path=$conf->crms_dir.$conf->req_dir."/".$year."/".$month."/Req".preg_replace('/\D*(\d+)\D*/',   '\\1',$req_id).".XLS";
      $req_url=$conf->req_dir."/".$year."/".$month."/Req".preg_replace('/\D*(\d+)\D*/',   '\\1',$req_id).".XLS";
      $sheet ="Req";
      try{
      //生成一个com对象$ex
      $ex = new COM("Excel.sheet") or Die ("连不上！！！");
      }catch(Exception $e){
          exit("<br>生成一个Excel对象失败，请重启服务器后再试!<hr>");
      }
     try {
     //打开一个excel文件
     $book = $ex->application->Workbooks->Open($workbook) or Die ("打不开！！！");
     $sheets = $book->Worksheets($sheet);
     $sheets->activate;
     ####################################
     ##以下将对Req表的单元格进行访问###
     //给该单元格调单号赋值
     $cell = $sheets->Cells(2,3);
     $cell->activate;
     $cell->value = mb_convert_encoding($data["Requisition_ID"],"GBK", "utf-8"); //把 UTF-8 编码字串转换成GBK编码字串
    //给该单元格主送赋值
     $cell = $sheets->Cells(4,2);
     $cell->activate;
     $cell->value =mb_convert_encoding($data["Main_Send"],"GBK", "utf-8");
     //给该单元格主调赋值
     $cell = $sheets->Cells(5,2);
     $cell->activate;
     $cell->value =mb_convert_encoding($data["Main_attemper"],"GBK", "utf-8");
    //给该单元格使用部门赋值
    $cell = $sheets->Cells(4,4);
    $cell->activate;
    $cell->value =mb_convert_encoding($data["Main_attemper"],"GBK", "utf-8");;
    //给该单元格要求完成的日期赋值
    $cell = $sheets->Cells(5,4);
    $cell->activate;
    $cell->value =mb_convert_encoding( $data["Complete_Time"],"GBK", "utf-8");
   //给该单元格事由赋值
   $cell = $sheets->Cells(6,2);
   $cell->activate;
   $cell->value = mb_convert_encoding($data["Cause"],"GBK", "utf-8");
   //给该单元格调度内容赋值
   $cell = $sheets->Cells(7,2);
   $cell->activate;
   $cell->value = mb_convert_encoding($data["Main_Content"],"GBK", "utf-8");
   //给该单元格拟稿人赋值
   $cell = $sheets->Cells(8,2);
   $cell->activate;
   $cell->value =mb_convert_encoding($data["Draft"].":".$data["Tel"],"GBK", "utf-8");
   //给该单元格通知时间赋值
   $cell = $sheets->Cells(8,4);
   $cell->activate;
   $cell->value =mb_convert_encoding($data["Notify_Time"],"GBK", "utf-8");
   //给该单元格联系人赋值
   $cell = $sheets->Cells(9,2);
   $cell->activate;
   $cell->value =mb_convert_encoding($data["linkman"],"GBK", "utf-8");
   //给该单元格抄送赋值
   $cell = $sheets->Cells(10,2);
   $cell->activate;
   $cell->value =mb_convert_encoding($data["To_1"],"GBK", "utf-8");
   //给该单元格抄报赋值
   $cell = $sheets->Cells(10,4);
   $cell->activate;
   $cell->value =mb_convert_encoding($data["To_2"],"GBK", "utf-8");
   //给该单元格路由赋值
   $cell = $sheets->Cells(11,2);
   $cell->activate;
   $cell->value =mb_convert_encoding($data["Router"],"GBK", "utf-8");

   ###############################
   //测试年目录
    if(!is_dir($req_year)){
      mkdir($req_year,0777);
      }
    //测试月目录
     if(!is_dir($req_month)){
      mkdir($req_month,0777);
     }
    //测试文件
    if(is_writeable($req_path)){
     unlink($req_path);
     }
      //保存为另一文件excel
    $ex->Application->ActiveWorkbook->SaveAs($req_path);
   //关掉excel，如果想看效果，则注释掉下面两行，由用户手动关掉excel
    $ex->Application->ActiveWorkbook->Close("False");
    unset ($ex);
    $reqdata = array(
               'File_Path' => $req_url,
            );
       $this->req->updateReq($id_,$reqdata);
    return $req_url;
    }catch(Exception $e) {
      //关掉excel，如果想看效果，则注释掉下面两行，由用户手动关掉excel
      $ex->Application->ActiveWorkbook->Close("False");
      unset ($ex);
      exit("生成调单文件的过程中出错，请重试！<hr>");
    }
  }
    function getReqID($id){
      $this->ifLogin();//登录认证
      $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
      $word_id=$this->Muser-> getUserWord($login);
      $datestring = "%Y";
      date_default_timezone_set('Asia/Shanghai');//北京时区
      $time = time();
      $this->load->helper('date');
      $year=trim(mdate($datestring, $time));
      $order_id=trim($this->req->getReqID($word_id."【".$year."】",$id));
      $req_id=$word_id."【".$year."】".$order_id."号";
      return trim($req_id); //输出调单编号
    }
   function getSimpleReqID($simpleID){
      $this->ifLogin();//登录认证
      $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
      if(!isset($simpleID)||$simpleID=="null"){
         $req_id="";
      }else{
        $word_id=$this->Muser-> getUserWord($login);
        $year=substr($simpleID, 0,4);
        $order_id=str_replace( $year,"",$simpleID);
        $req_id=$word_id."【".$year."】".$order_id."号";
      }
     echo $req_id;
    }
    function savereq(){
      $this->ifLogin();//登录认证
      $this->hasLevel("+R");//发送调单调度权根
      $id=trim($this->input->post("id"));
      if($id=='NULL'):
       $id=$this->req->setReq($this->getReqData());
      else:
       $this->req->updateReq($id,$this->getReqData());
     endif;
      echo $id;//返回添加的id
   }
   function download($id){
     settype($id,"integer");
     $this->ifLogin();//登录认证
     $this->load->helper('download');
     $req_path=$this->req->getPath($id);
    //if($req_path==""){ //如果没有下载地址，重新生成Excel。
    //  $req_id=$this->req->getReqId2($id);
    //  $this->setExcel($req_id, $id);
    // }
     $path= base_url().$req_path;
     $req_id= preg_replace('/\D*(\d+)\D*/',   '\\1',$this->req->getReqId2($id));//取数字部分
     $data = file_get_contents($path); // 读文件内容
     $name = "Req".$req_id.".XLS";
     force_download($name, $data);
   }
    function doreq(){
       $this->ifLogin();//登录认证
       $this->hasLevel("+R");//发送调单调度权根
       $id=trim($this->input->post("id"));
       //提交保存
      $sql_state="";
      if($id=='NULL'):
       $id=$this->req->setReq($this->getReqData());
      else:
       $this->req->updateReq($id,$this->getReqData());
      endif;
      //更新时间
        $datestring = " %Y-%m-%d %H:%i:%s";
        date_default_timezone_set('Asia/Shanghai');//北京时区
        $time = time();
        $this->load->helper('date');
        $update_time=mdate($datestring, $time);
        $req_id=$this->getReqID($id);
        //更新网调号和调单状态
        $reqdata = array(
               'Requisition_ID' => $req_id,
                 'state' =>'2',
               'update_time' =>$update_time,
            );
       $this->req->updateReq($id,$reqdata);
       //读取配置文件参数
        $conf = new conf();
        $CRMS_OS_sys=$conf->System_OS;
        $isSendMail=$conf->sendMail;
        //生成调单文件
       if($CRMS_OS_sys=="Windows"){
           $req_url=$this->setExcel_Win($req_id,$id);
       }else{
         $req_url="";  
       }
        
       //发送电子邮件
       if ($isSendMail){
       $addrs=$this->sendmail($id,$sql_state);
          if($addrs){
            $datas["mailinfo"]="邮件己发至：".$addrs.".";
          }else{ $datas["mailinfo"]="<B>邮件发送出错，请联系管理员.</B>";}
       }else{$datas["mailinfo"]="<B>邮件发送没有启用，请联系管理员.</B>";}
       //输出报告
       $this->parm_1();
       $datas["id"]=$id;
       $datas["req_id"]=$req_id;
       $datas["path"]=$req_url;
       $datas["update_time"]=$update_time;
       $this->load->view("req_rpt",$datas);
   }
   function req_cg(){
      $this->ifLogin();//登录认证
      $this->hasLevel("+R");//发送调单调度权根
      $this->parm_1();
      $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
      $org1_id=$this->Muser->getUserOrg1ID($login);
      $datas["cg"]=$this->req-> getRegCg($org1_id,$login);
      $this->load->view("req_cg",$datas);
   }
 function req_do($id=0){
    $this->ifLogin();//登录认证
    $this->hasLevel("+R");//发送调单调度权根
    settype($id,"integer"); //设$id为整型
    $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
    $org1_id=$this->Muser->getUserOrg1ID($login);
    $this->load->library('pagination');
    //自定义分页
    $config['first_link'] = '第一页';
    $config['last_link'] = '最后一页';
    $config['next_link'] = '下一页';
    $config['prev_link'] = '上一页';
    $config['cur_tag_open'] = '<b>&nbsp;';
    $config['cur_tag_close'] = '&nbsp;</b>';
    $config['full_tag_open'] = '页数：';
    $config['base_url'] = site_url("main/req_do");
    $config['total_rows'] = $this->req->getTotalDo($org1_id,$login);
    $config['per_page'] = '10';
    //初始化分页参数
    $this->pagination->initialize($config);
    $datas['pages']=$this->pagination->create_links();
    $this->parm_1();
    $datas["do"]=$this->req-> getRegDo($org1_id,$login,$id,10);
    $this->load->view("req_do",$datas);
   }
   function req_ygd($id=0){
    $this->ifLogin();//登录认证
    $this->hasLevel("+Q");//发送调单调度权根
    settype($id,"integer"); //设$id为整型
    $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
    $org1_id=$this->Muser->getUserOrg1ID($login);
    $this->load->library('pagination');
    //自定义分页
    $config['first_link'] = '第一页';
    $config['last_link'] = '最后一页';
    $config['next_link'] = '下一页';
    $config['prev_link'] = '上一页';
    $config['cur_tag_open'] = '<b>&nbsp;';
    $config['cur_tag_close'] = '&nbsp;</b>';
    $config['full_tag_open'] = '页数：';
    $config['base_url'] = site_url("main/req_ygd");
    $config['total_rows'] = $this->req->getTotalYgd($org1_id);
    $config['per_page'] = '10';
    //初始化分页参数
    $this->pagination->initialize($config);
    $datas['pages']=$this->pagination->create_links();
    $this->parm_1();
    $datas["ygd"]=$this->req-> getRegYgd($org1_id,$id,10);
    $this->load->view("req_ygd",$datas);
   }
   function req_all($id=0){
    $this->ifLogin();//登录认证
    $this->hasLevel("+Q");//发送调单调度权根
    settype($id,"integer"); //设$id为整型
    $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
    $org1_id=$this->Muser->getUserOrg1ID($login);
    $this->load->library('pagination');
    //自定义分页
    $config['first_link'] = '第一页';
    $config['last_link'] = '最后一页';
    $config['next_link'] = '下一页';
    $config['prev_link'] = '上一页';
    $config['cur_tag_open'] = '<b>&nbsp;';
    $config['cur_tag_close'] = '&nbsp;</b>';
    $config['full_tag_open'] = '页数：';
    $config['base_url'] = site_url("main/req_all");
    $config['total_rows'] = $this->req->getTotalAll($org1_id);
    $config['per_page'] = '10';
    //初始化分页参数
    $this->pagination->initialize($config);
    $datas['pages']=$this->pagination->create_links();
    $this->parm_1();
    $datas["ygd"]=$this->req-> getRegAll($org1_id,$id,10);
    $this->load->view("req_all",$datas);
   }
   function req_del($id,$rd_url){
     $this->ifLogin();//登录认证
     $this->hasLevel("+R");//发送调单调度权根
     settype($id,"integer");//设为整型，防SQL注入
     $this->req->delReqbyID($id);
     redirect("main/".$rd_url);
   }
   function req_gd($id){
     $this->ifLogin();//登录认证
     $this->hasLevel("+S");//发送调单调度权根
     settype($id,"integer");//设为整型，防SQL注入
     $this->parm_1();
     $datas["req"]=$this->req->getReqByOrderID3($id);
     $this->load->view('req_gd',$datas);
   }
   function  savereq_gd(){
     $this->ifLogin();//登录认证
     $this->hasLevel("+S");//发送调单归档权根
     $id=$text=trim($this->input->post("id"));
     settype($id,"integer");
     $text=trim($this->input->post("text"));
     $this->req->savereq_gd($text,$id);
     echo "调单归档成功！";
   }
   function getCgTotal(){
     $this->ifLogin();//登录认证
     $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
     $org1_id=$this->Muser->getUserOrg1ID($login);
     if($this->lev->hasLev($login,"+R")):
      echo "(". $this->req->getTotalCg($org1_id,$login).")";
     else:
      echo "";
     endif;
   }
    function getDoTotal(){
       $this->ifLogin();//登录认证
       $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
       $org1_id=$this->Muser->getUserOrg1ID($login);
       if($this->lev->hasLev($login,"+R")):
         echo "(". $this->req->getTotalDo($org1_id,$login).")";
        else:
         echo "";
       endif;
   }
   function req_find(){
     $this->ifLogin();//登录认证
     $this->hasLevel("+Q");//发送查询权根
     $this->parm_1();
     $data["type"]=$this->tpe->getTypeAll();
     $this->load->view('req_find',$data);
   }
   function req_find_rst(){
     $this->ifLogin();//登录认证
     $this->hasLevel("+Q");//发送查询权根
     $this->parm_1();
     $req_id=trim($this->input->post("req_id"));
     $draft=trim($this->input->post("draft"));
     $router=trim($this->input->post("router"));
     $send=trim($this->input->post("send"));
     $notify=trim($this->input->post("notify"));
     $mon=trim($this->input->post("mon"));
     $type=trim($this->input->post("type"));
     $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
     $org1_id=$this->Muser->getUserOrg1ID($login);
     $data["req"]=$this->req->getFind($org1_id,$req_id,$draft,$router,$send,$notify,$mon,$type);
     $data["total"]=$this->req->getTotalFind($org1_id,$req_id,$draft,$router,$send,$notify,$mon,$type);
     $this->load->view('req_find_rst',$data);
   }
   function req_browser(){
     $this->ifLogin();//登录认证
     $this->hasLevel("+Q");//发送查询权根
     $this->parm_1();
     $req_id=trim($this->input->post("req_id"));
     $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
     $org1_id=$this->Muser->getUserOrg1ID($login);
     $data["ids"]=$this->req->getRegAllbyBrowser($org1_id);
     $data["req"]=$this->req-> getReqByID($req_id);
     $this->load->view('req_browser',$data);
   }
   function req_update($id,$type_id){
      $this->ifLogin();//验证用户登录
      $this->hasLevel("+R");//发送调单调度权根
      $this->parm_1();
      $req_login=$this->req->getReqLoginByID($id);
      settype($type_id,"integer");
      settype($id,"integer");
      $datas["type"]=$this->tpe->getTypeBy($type_id);
      if($id!="NULL"){ $datas["req"]=$this->req->getReqByOrderID2($id);}
      $this->load->view('req_update',$datas);
   }
   function save_req_upadte(){
       $this->ifLogin();//登录认证
       $this->hasLevel("+R");//发送调单调度权根
       $id=trim($this->input->post("id"));
       $req_id=trim($this->input->post("req_id"));
       $org1_id=trim($this->input->post("org1_id"));
       $us_login=trim($this->input->post("user_login"));
       $state=trim($this->input->post("state"));
       $type=trim($this->input->post("type_id"));
       $cell_id=trim($this->input->post("cell_id"));
       $mon=trim($this->input->post("mon"));
       $main_send=trim($this->input->post("send"));
       $main_att=trim($this->input->post("mainsend"));
       $cause=trim($this->input->post("cause"));
       $main_content=trim($this->input->post("text"));
       $draft=trim($this->input->post("draft"));
       $tel=trim($this->input->post("tel"));
       $notify_time=trim($this->input->post("d1"));
       $complete_time=trim($this->input->post("d2"));
       $linkman=trim($this->input->post("linkman"));
       $to_1=trim($this->input->post("to1"));
       $to_2=trim($this->input->post("to2"));
       $router=trim($this->input->post("router"));
       $chk=trim($this->input->post("checkbox"));
       $file_path="";//重置调单文件
       //生成数组
       $reqdata = array(
               'Requisition_ID' => $req_id,
               'state' => $state,
               'type' => $type,
               'Cell_ID' => $cell_id,
               'org1_id'=>$org1_id,
               'us_login'=>$us_login,
               'Req_Mon'=>$mon,
               'Main_Send' =>$main_send,
               'Main_attemper' => $main_att,
                'Cause' => $cause,
               'Main_Content' => $main_content,
               'Draft' => $draft,
               'Tel' => $tel,
               'Notify_Time' => $notify_time,
               'Complete_Time' => $complete_time,
               'linkman' => $linkman,
               'To_1' => $to_1,
               'To_2' => $to_2,
               'Router' => $router,
               'File_Path' => $file_path,
            );
            $this->req->updateReq($id,$reqdata);
            $this->setExcel($req_id,$id);
            $mailinfo="";
            //发送邮件通知
            if($chk=="1"){
                $addrs=$this->sendmail($id, "变更:");
                if( $addrs){
                    $mailinfo="邮件己发送至：".$addrs.".";
                 }else{
                    $mailinfo="邮件通知发送失败，请通知管理员.";
                 }
            }
            $down="<a href='".site_url('main/download'.'/'.$id)."' target=''><img src='images/zip.gif' width='16' height='16'  border='0' title='下载文件' /></a>";
            echo $req_id."调单资料更新成功!  ".$down."<br>".$mailinfo;
   }
   function bts_import(){
       $this->ifLogin();//登录认证
       $this->hasLevel("+I");//发送数据导入权根
       $this->parm_1();
       $this->load->view('bts_import');
   }
   function getmailattr($id){
     $this->ifLogin();//登录认证
     $this->hasLevel("+Q");//发送查询权根
     $attr=$this->ml->getattr($id);
     //echo $attr;
     return $attr;

   }
   function getmailaddr($id){
     $this->ifLogin();//登录认证
     $this->hasLevel("+Q");//发送查询权根
     $attr=$this->ml->getmails($id);

     //额外添加列表
     $list = array('www@163.com',//example
     );
     foreach($list as $row){
       $attr.=$row.",";//添加
    }

     //免打扰列表
     $list = array('www@163.com',//example
                    'mazhongying.hh@yn.chinamobile.com',
                    'qiuhong.hh@yn.chinamobile.com',
                    'zouxiang.hh@yn.chinamobile.com');
     foreach($list as $row){
       $attr=str_replace($row.",","",$attr);//去除
    }
     $attr=trim(substr($attr,0,strlen($attr)-1));//去掉最后的逗号
    // $a="lichaoyan.hh@yn.chinamobile.com";
     return $attr;
   }
   function sendmail($id,$sql_state){
       $this->ifLogin();//登录认证
       $this->hasLevel("+R");//发送调单调度权根
       $config = Array(
 		    'protocol' => 'smtp',
  		    'smtp_host' => 'mail.yncmcc.com',
  		    'smtp_port' => 25,
 		    'smtp_user' => 'lichaoyan.hh',
 	        'smtp_pass' => 'yahoo2009',
            'charset' => 'GB2312',
 		    'mailtype' => 'html',
 		);
      // $config_ssl = Array(
		  // 'protocol' => 'smtp',
		  //  'smtp_host' => 'ssl://smtp.gmail.com',
		  //  'smtp_port' => '465',
		  // 'smtp_user' => 'kissmumu@gmail.com',
		  //  'smtp_pass' => '4inlove828830',
		  //  'mailtype' => 'html',
	 //	);
        $login=$this->SQLsafe2($this->session->userdata('crms_username_auth'));
        $from_name = $this->ml->getUserName($login);//发件人名称
        $this->load->library('email', $config);
		$this->email->set_newline("\r\n");
        $addr=$this->getmailaddr($id);
        $email_subject =$sql_state."本地网传输调单（调单号：".$this->ml-> getreqID($id)."），请尽快办理！";
        $email_msg="<br>你好！请注意查收！<br>
                                        <br>=====================================<br>
                                         注：此邮件是由“红河移动传输资源信息系统”
                                         自动发送，<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>---请勿回复,谢谢！</b>
                                         <br><br>系统网址：
                                         <A target='_blank' href='http://:10.171.61.92'>http://:10.171.61.92</A>
                                        <br>=====================================<br>";
        //解决乱码问题
        $from_name = iconv('UTF-8','GB2312',$from_name);
        $email_subject = iconv('UTF-8','GB2312',$email_subject);
        $email_msg = iconv('UTF-8','GB2312',$email_msg);
        //封装发送信息
        $this->email->from($this->ml->getUserEmail($login),$from_name);
        $this->email->to($addr.",".$this->ml->getUserEmail($login));//取地址
        $this->email->subject($email_subject);
        $this->email->message($email_msg);
        $this->email->attach($this->getmailattr($id));//附加调单附件
        //发送
       if (!$this->email->send())
		{
		    show_error($this->email->print_debugger());
		    return false;
		}
		else
		{
		    return $addr;
		}
   }
   function logout(){
        $this->evt->setUserEvent("注销成功");
		$this->session->unset_userdata('crms_username_auth');
        $this->session->unset_userdata('crms_userpwd_auth');
		redirect('main/login');
	}
}//end class

?>